<template>
  <div>
    <el-dialog :title="dialogData.title"
               :visible.sync="dialogData.show"
               center
               width="50%"
               @close="$emit('closedialog')">
      <el-form :model="formdata"
               label-position="right"
               label-width="150px"
               ref="formRefs"
               :rules="formRules">
        <el-form-item label="名称："
                      prop="name">
          <el-input v-model="formdata.name"
                    maxlength="20"
                    autocomplete="off"
                    clearable
                    placeholder="请输入名称"
                    style="width:50%"></el-input>
        </el-form-item>
        <el-form-item label="服务有效期(月)："
                      prop="validPeriod">
          <el-input v-model="formdata.validPeriod"
                    maxlength="10"
                    type="number"
                    autocomplete="off"
                    style="width:50%"
                    clearable
                    placeholder="单位（月）"></el-input>
        </el-form-item>
        <el-form-item label="图片："
                      prop="imageUrl">
          <el-upload class="avatar-uploader"
                     :action="imageUploadUrl"
                     :show-file-list="false"
                     accept=".JPG, .PNG, .JPEG,.jpg, .png, .jpeg"
                     :on-success="handleAvatarSuccess"
                     :before-upload="beforeAvatarUpload">
            <img v-if="formdata.imageUrl"
                 :src="formdata.imageUrl"
                 class="avatar">
            <i v-else
               class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="适用人群："
                      prop="applicableThrong">
          <el-input v-model="formdata.applicableThrong"
                    maxlength="50"
                    autocomplete="off"
                    clearable
                    placeholder="请输入适用人群"
                    style="width:50%"></el-input>
        </el-form-item>
        <el-form-item label="面诊预约次数："
                      prop="faceConsultationCount">
          <el-input v-model="formdata.faceConsultationCount"
                    type="number"
                    autocomplete="off"
                    clearable
                    placeholder="请输入面诊预约次数"
                    style="width:50%"></el-input>
        </el-form-item>
        <el-form-item label="报告解读次数："
                      prop="reportInterpretation">
          <el-input v-model="formdata.reportInterpretation"
                    type="number"
                    autocomplete="off"
                    clearable
                    placeholder="请输入报告解读次数"
                    style="width:50%"></el-input>
        </el-form-item>
        <el-form-item label="服务内容："
                      prop="content">
          <el-input v-model="formdata.content"
                    autocomplete="off"
                    show-word-limit
                    maxlength="250"
                    rows="4"
                    clearable
                    type="textarea"
                    placeholder="请输入服务内容"
                    style="width:100%"></el-input>
        </el-form-item>
        <el-form-item label="服务价格："
                      prop="hospitalDtoList">
          <el-button type="primary"
                     @click="handleAdd">新 增 一 行</el-button>
          <el-table :data="formdata.hospitalDtoList"
                    style="width: 100%"
                    height="250"
                    border
                    class="mt-20">
            <el-table-column prop="hospitalId"
                             label="医院名称">
              <template slot-scope="scoped">
                <el-form-item :rules="listRules.hospitalId"
                              :prop="`hospitalDtoList.${scoped.$index}.hospitalId`">
                  <el-select v-model="scoped.row.hospitalId"
                             value-key="hospitalId"
                             filterable
                             remote
                             clearable
                             :loading="loading1"
                             placeholder="请选择医院"
                             :remote-method="gethospitalList">
                    <el-option v-for="item in options1"
                               :label="item.name"
                               :value="item.hospitalId"
                               :key="item.hospitalId">
                    </el-option>
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="price"
                             label="价格（元）">
              <template slot-scope="scoped">
                <el-form-item :rules="listRules.price"
                              :prop="`hospitalDtoList.${scoped.$index}.price`">
                  <el-input v-model="scoped.row.price"
                            placeholder="请输入价格"
                            type="number"
                            clearable
                            maxlength="8"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="option"
                             label="操作"
                             align="center"
                             width="120">
              <template slot-scope="scoped">
                <el-button type="danger"
                           @click='deleteOne(scoped.$index, formdata.hospitalDtoList)'>删 除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <!-- <el-form-item label="指标解读价格设置：" prop="readPriceDtoList">
                <div v-for="it in  formdata.readPriceDtoList" :key='it.id' style="width:25%;float:left;margin-bottom:5px">
                    {{it.doctorTitleName}}：<el-input v-model="it.price"  autocomplete="off"  style="width:50%;" type="number" placeholder="单位（元）"></el-input>
                </div>
            </el-form-item> -->
        <el-form-item label="每单推广费用："
                      prop="extendFee">
          <el-input v-model="formdata.extendFee"
                    maxlength="8"
                    type="number"
                    autocomplete="off"
                    clearable
                    placeholder="请输入每单推广费用"
                    style="width:50%"></el-input>
        </el-form-item>
        <el-form-item label="购买服务后，是否填写健康服务号："
                      label-width="240px">
          <el-radio-group v-model="formdata.isInvitationCode">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <div style="text-align:center">
          <el-button @click="$emit('closedialog')">取 消</el-button>
          <el-button type="primary"
                     @click="onSubmit">保 存</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    props: ['dialogData'],
    data () {
      return {
        formRules: {
          name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
          validPeriod: [{ required: true, message: '请填写服务有效期', trigger: 'blur' }],
          imageUrl: [{ required: true, message: '请填上传图片', trigger: 'blur' }],
          applicableThrong: [{ required: true, message: '请填写适用人群', trigger: 'blur' }],
          faceConsultationCount: [{ required: true, message: '请填写面诊预约次数', trigger: 'blur' }],
          reportInterpretation: [{ required: true, message: '请填写报告解读次数', trigger: 'blur' }],
          content: [{ required: true, message: '请填写服务内容', trigger: 'blur' }],
          extendFee: [{ required: false, message: '请填写服务内容', trigger: 'blur' }],
          hospitalDtoList: [{ required: true, message: '请填配置服务价格', trigger: 'blur' }],
        },
        listRules: {
          hospitalId: [{ required: true, message: '请选择医院', trigger: ['blur', 'change'] }],
          price: [{ required: true, message: '请输入价格', trigger: ['blur', 'change'] }],
        },
        formdata: {
          type: 1,
          name: '',
          validPeriod: '', // 服务有效期
          imageUrl: null, // 图片
          applicableThrong: '', // 适用人群
          faceConsultationCount: '', // 面诊预约次数
          reportInterpretation: '', // 报告解读次数
          content: '', // 服务内容
          hospitalDtoList: [], // 服务价格
          isInvitationCode: 0, // 购买服务后，是否填写健康服务号
        },
        options1: [],
        loading1: false,
        imageUploadUrl: this.api.appUpgradeUpload.url,
      }
    },
    methods: {
      onSubmit () {
        this.$refs.formRefs.validate((valid) => {
          if (valid) {
            if (this.formdata.extendFee == 0) {
              this.formdata.extendFee = undefined;
            }
            if (this.formdata.extendFee) {
              this.formdata.way = 1;
            } else {
              this.formdata.way = null;
            }
            this.post(this.api.cooperationHospital.uploadHealth, this.formdata).then(res => {
              if (res.data.code === '200') {
                this.$message.success('保存成功!');
                this.$emit('closedialog');
              }
            })
          }
        })
      },
      handleAdd () {
        this.formdata.hospitalDtoList.push({
          hospitalId: null,
          price: '',
        })
      },
      deleteOne (index, row) {
        row.splice(index, 1);
      },
      gethospitalList (query) {
        if (query !== '') {
          this.loading1 = true;
          this.post(this.api.cooperationHospital.getHospitalList, {
            pageSize: 100,
            pageIndex: 0,
            searchCondition: query,
            selectHospitalIds: []
          }).then(res => {
            if (res.data.code === '200') {
              this.loading1 = false;
              this.options1 = res.data.object;
            }
          });
        } else {
          this.options1 = [];
        }
        this.loading1 = false;
      },

      handleAvatarSuccess (res) {
        this.formdata.imageUrl = res.object[0];
      },
      beforeAvatarUpload (file) {
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!');
        }
        return isLt2M;
      },
    },
    created () {
      if (this.dialogData.info) {
        this.post(this.api.cooperationHospital.getHealthById, { id: this.dialogData.info.id }).then(res => {
          if (res.data.code === '200') {
            let resData = res.data.object;
            this.formdata = resData;
            if (resData.hospitalDtoList) {
              this.formdata.hospitalDtoList.forEach(v => {
                this.gethospitalList(v.hospitalName);
              })
            } else {
              this.formdata.hospitalDtoList = [];
            }
          }
        })
      }
    }
  }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 98px !important;
    height: 98px !important;
    line-height: 98px !important;
    text-align: center;
  }
  .avatar {
    width: 98px !important;
    height: 98px !important;
    display: block;
  }
  .ivu-select-dropdown {
    z-index: 1000 !important;
  }
</style>